import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class test {
    static Student[] stus= new Student[]{
            new Student("小小1","女","500801"),
            new Student("小小2","女","500802"),
            new Student("小小3","女","500803"),
            new Student("小小4","女","500804"),
            new Student("小小5","女","500805"),
            new Student("小小6","女","500806"),
            new Student("小小7","女","500807"),
    };

    public static void main(String[] args) throws IOException, ClassNotFoundException {

        readText();
        List<Student> list =new ArrayList<Student>();
        for (Student stu : stus)
            list.add(stu);
        //打印list容器里的学生对象信息
//        System.out.println("list容器里的学生对象信息:");
//        for (int i = 0; i < list.size(); i++)
//            System.out.println(list.get(i).toString());

        //读入和写入文本
        writeStudentsToTextFile("student.txt",list);
//        readStudentsFromTextFile("student.txt");
        System.out.println("以文本形式读出的学生信息：");
        for (int i = 0; i < readStudentsFromTextFile("student.txt").size(); i++)
            System.out.println(list.get(i).toString());

        //以二进制读入和写入
//        System.out.println("以二进制形式读出的学生信息：");
//        writeStudentsToBinaryFile("student.bin",list);
//        readStudentsFromBinaryFile("student.bin");
    }


    public static void writeStudentsToTextFile
            (String fn,List<Student> students) throws FileNotFoundException, UnsupportedEncodingException {
        File file= new File(fn);
        PrintWriter writer = new PrintWriter(file,"utf-8");
        for(Student st: students){
            writer.print(st.toString());
            writer.println(";");
        }
        writer.close();
    }

    public static List<Student> readStudentsFromTextFile(String fn) throws FileNotFoundException{
        File file =new File(fn);
        List<Student> sts = new ArrayList<Student>();
        Scanner scan = new Scanner(file,"utf-8");
        String str= "";
        // scan.useDelimiter("[,;]");
        while(scan.hasNext()){
            // Student.parseStudent(scan.next());
            //String name = scan.next();
            //String sex= scan.next();
            //String id =scan.next();
            scan.nextLine();
            //Student st =new Student(name,sex,id);
            str = scan.next();
//            System.out.println("next  "+str);
            sts.add(Student.parseStudent(str));
        }
        scan.close();
        return sts;

    }

    public static void writeStudentsToBinaryFile(String fn,List<Student> students) throws IOException{
        FileOutputStream file= new FileOutputStream(fn);
        ObjectOutputStream writer = new ObjectOutputStream(file);
        List<Student> al = new ArrayList<Student>();
        for(Student st:students)
            al.add(st);
        writer.writeObject(al);
        writer.close();
    }

    public static void readStudentsFromBinaryFile(String fn) throws ClassNotFoundException, IOException{
        FileInputStream file = new FileInputStream(fn);
        ObjectInputStream reader =new ObjectInputStream(file);
        @SuppressWarnings("unchecked")
        ArrayList<Student> al = (ArrayList<Student>)reader.readObject();
        reader.close();
        for(Student st:al){
            System.out.println(st);
        }
    }

    public static String readText(){
        File file = new File("json_demo.txt");
        StringBuilder result = new StringBuilder();
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String s = null;
            while((s = br.readLine())!=null){//使用readLine方法，一次读一行
                result.append(System.lineSeparator()+s);
            }
            br.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(result.toString());
        return result.toString();
    };

}

class Student implements Serializable {
    private String Name;
    private String Sex;
    private String ID;

    public Student(){};

    public Student(String name,String sex,String id){
        this.Name=name;
        this.Sex=sex;
        this.ID=id;
    }

    public String getName(){
        return Name;
    }

    public String getSex(){
        return Sex;
    }
    public String getID(){
        return ID;
    }
    public void setName(String name){
        this.Name = name;
    }

    public void setSex(String sex){
        this.Sex = sex;
    }

    public void setID(String id){
        this.ID = id;
    }
    @Override
    public String toString(){
        return String.format("Name:%s,Sex:%s,ID:%s",
                getName(),getSex(),getID());
    }

    public static Student parseStudent(String s){
        String[] ArrayStu =s.split(",");
        Student st = new Student(ArrayStu[0],ArrayStu[1],ArrayStu[2]);
        return st;
    }
}


